#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
    ListNode *removeNthFromEnd(ListNode *head, int n)
    {
        ListNode *ptr_fast = head, *ptr_slow = head;
        for (int i = 0; i < n; ++i)
        {
            ptr_fast = ptr_fast->next;
        }
        if (ptr_fast == nullptr)
            return head->next;
        while (ptr_fast->next != nullptr)
        {
            ptr_slow = ptr_slow->next;
            ptr_fast = ptr_fast->next;
        }
        ptr_slow->next = ptr_slow->next->next;
        return head;
    }
};